package xuan.gui;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;

import javax.swing.JButton;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import xuan.lib.Transfer;

/**
 * Open File Window used to select files from system
 * 
 * @author xuan
 *
 */
public class OpenFileWindow extends JDialog implements ActionListener{
	private JPanel panel;
	private JTextField txtSourceFile;
	private JTextField txtTargetFile;
	private JTextField txtEditLog;
	private JButton btnSourceFile;
	private JButton btnTargetFile;
	private JButton btnEditLog;
	private JButton btnOk;
	private JButton btnCancel;
	private boolean okPressed;
	
	public OpenFileWindow(JFrame parent) {
		super(parent, "Connect", true);
		
		setMinimumSize(new Dimension(500, 170));
		setTitle("Open");
		setPreferredSize(new Dimension(500, 170));
		
		panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		
		JLabel lblNewLabel = new JLabel("Source File");
		panel.add(lblNewLabel);		
		txtSourceFile = new JTextField();
		txtSourceFile.setToolTipText("");
		panel.add(txtSourceFile);
		txtSourceFile.setColumns(25);		
		btnSourceFile = new JButton("Browser");
		btnSourceFile.addActionListener(this);
		panel.add(btnSourceFile);
		
		JLabel lblTargetFile = new JLabel("Target File");
		panel.add(lblTargetFile);	
		txtTargetFile = new JTextField();
		txtTargetFile.setToolTipText("");
		txtTargetFile.setColumns(25);
		panel.add(txtTargetFile);
		btnTargetFile = new JButton("Browser");
		btnTargetFile.addActionListener(this);
		panel.add(btnTargetFile);
		
		JLabel lblEditLog = new JLabel("Edit Log     ");
		panel.add(lblEditLog);	
		txtEditLog = new JTextField();
		txtEditLog.setToolTipText("");
		txtEditLog.setColumns(25);
		panel.add(txtEditLog);
		btnEditLog = new JButton("Browser");	
		btnEditLog.addActionListener(this);
		panel.add(btnEditLog);
		
		btnOk = new JButton("OK");
		btnOk.setPreferredSize(new Dimension(81, 25));
		btnOk.addActionListener(this);
		panel.add(btnOk);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(this);
		panel.add(btnCancel);
		
		setLocationRelativeTo(null);
	}
	

	@Override
	public void actionPerformed(ActionEvent evt) {
		Object button = evt.getSource();
		if (button == btnOk) {
			if(!txtSourceFile.getText().isEmpty() && !txtTargetFile.getText().isEmpty() && !txtEditLog.getText().isEmpty()){
				okPressed = true;
			    setVisible(false);
			}
			else{
				JOptionPane.showConfirmDialog(panel,"files missed !!!","",JOptionPane.CLOSED_OPTION);
			}
		}
		if (button == btnCancel){
			setVisible(false);
		}
		if(button==btnSourceFile){
			JFileChooser chooser = new JFileChooser();
			chooser.showOpenDialog(panel);
			try{
				txtSourceFile.setText(chooser.getSelectedFile().getAbsolutePath());
			}
			catch(Exception e){}
    	}
    	if(button==btnTargetFile){
    		JFileChooser chooser = new JFileChooser();
			chooser.showOpenDialog(panel);
			try{
				txtTargetFile.setText(chooser.getSelectedFile().getAbsolutePath());
			}
			catch(Exception e){}
    	}
    	if(button==btnEditLog){
    		JFileChooser chooser = new JFileChooser();
			chooser.showOpenDialog(panel);
			try{
				txtTargetFile.setText(chooser.getSelectedFile().getAbsolutePath());
			}
			catch(Exception e){}
    	}
	}
	
	
	  public boolean showDialog(Transfer transfer) {		  	
		  	okPressed = false;
		  	show();
		    if (okPressed) {
		    	transfer.source = txtSourceFile.getText();
		    	transfer.target = txtTargetFile.getText();
		    	transfer.edit = txtEditLog.getText();
		    }
		    
		    return okPressed;
	  }
}
